﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;


public partial class AdminX_Company_LiveSupportMan : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Alert1.HideMessage();
        //        Page.Title = MultipleLanguage.OWNSITE + " - " + MultipleLanguage.ADV.ToLower();
        if (!IsPostBack)
        {
            this.ShowMess();
            this.BindDataToPositionDropdown();
            this.BindDataToGrid(1);
        }

    }

    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLang];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region BindDataToDropDown
    private void BindDataToPositionDropdown()
    {
        AdvPosition objPosition = new AdvPosition();
        DataTable dtbPosition = new DataTable();
        dtbPosition = objPosition.Search();
        ddlPosition.DataSource = dtbPosition;
        ddlPosition.DataValueField = "AdvPositionID";
        ddlPosition.DataTextField = "Description";
        ddlPosition.DataBind();
        Utl.SetNonSelectToControl(ddlPosition, "-------- Tất cả --------", "------ Rỗng ------");
    }


    #endregion






    #region Grid Processing
    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nPageCount = 0;
        int nPosition = ConvertData.ConvertToInt(ddlPosition.SelectedValue);
        int nStatus = ConvertData.ConvertToInt(ddlStatus.SelectedValue);
        int nItemView = 0;
        int nType = 1;
        string sTitle = ConvertData.ConvertStringToDB(txtKeyword.Text.Trim());
        Adv objAdv = new Adv();
        DataTable dtbAdv = new DataTable();
        dtbAdv = objAdv.Search(nItemView, sTitle, nPosition, nType, nStatus, CurrentPageIndex, Config.PAGESIZE, ref nPageCount);
        lblTotalRecord.Text = "Có" + " " + nPageCount + " " + "bản ghi được tìm thấy.";
        dtgAdv.DataSource = dtbAdv;
        dtgAdv.DataBind();

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);
        if (dtbAdv.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;

        txtKeyword.Focus();
        txtKeyword.Text = ConvertData.RemoveHTMLTag(txtKeyword.Text.Trim());

    }

    protected void dtgAdv_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblPartner = (Label)e.Item.FindControl("lblPartner");
            Label lblPosition = (Label)e.Item.FindControl("lblPosition");
            Label lblDateCreated = (Label)e.Item.FindControl("lblDateCreated");
            Image imgImage = (Image)e.Item.FindControl("imgImage");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_ADV_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvFile"));
            lblPartner.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "PartnerName")) + ". " + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvURL"));
            lblPosition.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvPositionDescription"));
            lblDateCreated.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));

            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLang]);
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLang]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLang]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AdvID"));
        }
    }

    protected void dtgAdv_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Adv objAdv = new Adv();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objAdv.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLang]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLang]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objAdv.UpdateStatus(nID, EnumeType.INACTIVE);
                if (nActive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLang]);

                else
                    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLang]);
                BindDataToGrid(1);
                break;

            case "Inactive":
                int nInactive = objAdv.UpdateStatus(nID, EnumeType.ACTIVE);
                if (nInactive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLang]);
                else
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLang]);
                BindDataToGrid(1);
                break;
        }
    }

    #endregion

    #region Click Events
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }
    protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }
    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ADV_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    #endregion
}